<template>
  <div class="page-store-billing-trading">
    <div class="search-bar">
      <el-form
        clearable
        :inline="true"
        :model="dataFrom"
        class="demo-form-inline from"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('chat.orderNumber') + ':'">
            <el-input
              v-model="dataFrom.orderNumber"
              maxlength="20"
              onkeyup="value=value.replace(/[^\d]/g,'')"
              clearable
              :placeholder="$t('chat.orderNumber')"
            />
          </el-form-item>
          <el-form-item :label="$t('order.refundId') + ':'">
            <el-input
              v-model="dataFrom.refundSn"
              maxlength="19"
              onkeyup="value=value.replace(/[^\d]/g,'')"
              clearable
              :placeholder="$t('order.refundId')"
            />
          </el-form-item>
          <el-form-item :label="$t('shop.ioType') + ':'">
            <el-select
              v-model="dataFrom.ioType"
              clearable
              :placeholder="$t('shop.ioType')"
            >
              <el-option
                v-for="item in ioType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('shop.amountType') + ':'">
            <el-select
              v-model="dataFrom.amountType"
              clearable
              :placeholder="$t('shop.amountType')"
            >
              <el-option
                v-for="item in amountType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('shop.reason') + ':'">
            <el-select
              v-model="dataFrom.reason"
              clearable
              :placeholder="$t('shop.reason')"
            >
              <el-option
                v-for="item in reason"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSubmit(true)"
            >
              {{ $t('order.query') }}
            </div>
            <div
              class="default-btn"
              @click="clear"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          :data="tableData"
          style="width: 100%"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
        >
          <el-table-column
            type="index"
            :label="$t('formData.serialNumber')"
            align="left"
            width="85"
          />
          <el-table-column
            prop="amountType"
            :label="$t('shop.amountType')"
            width="120"
          >
            <template #default="scope">
              <span>{{ [$t('shop.amoSett'),$t('distribUserWallet.withdrawalAmount'),$t('shopWallet.unusableBalance'),$t('shop.summAmo')][scope.row.amountType] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="changeAomunt"
            :label="$t('shop.changeAomunt')"
            width="120"
          >
            <template #default="scope">
              <span>{{ ['-','+'][scope.row.ioType] }} ￥{{ Math.abs(scope.row.changeAomunt) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="orderNumber"
            :label="$t('chat.orderNumber')"
            align="left"
            width="220"
          >
            <template #default="scope">
              {{ scope.row.orderNumber ? scope.row.orderNumber : '-' }}
            </template>
          </el-table-column>
          <el-table-column
            prop="refundSn"
            :label="$t('order.refundId')"
            width="220"
          >
            <template #default="scope">
              <span>{{ scope.row.refundSn ? scope.row.refundSn : '-' }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column
          prop="ioType"
          label="收支类型"
          width="120"
        >
          <template slot-scope="scope">
            <span>{{ ['支出','收入'][scope.row.ioType] }}</span>
          </template>
        </el-table-column> -->
          <el-table-column
            prop="userAmount"
            :label="$t('shopWallet.operationAmount')"
            width="140"
          >
            <template #default="scope">
              <span v-if="scope.row.userAmount === 0 || !scope.row.userAmount">0</span>
              <span v-else>{{ ['-','+'][scope.row.ioType] }}{{ scope.row.userAmount || 0 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="platformAmount"
            :label="$t('settlementDetail.platformShareReduce')"
            width="150"
          >
            <template #default="scope">
              <span>￥ {{ scope.row.platformAmount || 0 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopReduceAmount"
            :label="$t('settlementDetail.multishopReduce')"
            width="150"
          >
            <template #default="scope">
              <span>￥ {{ scope.row.shopReduceAmount || 0 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="distributionAmount"
            :label="$t('settlementDetail.distributionAmount')"
            min-width="130"
          >
            <template #default="scope">
              <span>￥ {{ scope.row.distributionAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="platformCommission"
            :label="$t('shopWallet.platformCommission')"
            min-width="130"
          >
            <template #default="scope">
              <span>￥ {{ scope.row.platformCommission || 0 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="reason"
            min-width="150"
            align="left"
            :label="$t('shopWallet.amountChangeReason')"
          >
            <template #default="scope">
              <span>{{ [$t('shop.userPay'),$t('shopWallet.orderSettlement'),$t('shopWallet.applyRefund'),$t('shopWallet.rejectApplyRefund'),$t('shop.withApply'),
                        $t('shop.withNoPass'),$t('shop.withPass')][scope.row.reason] }}</span>
              <!-- ,$t('shop.succIss'),$t('shop.failIss') -->
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            width="150"
            :label="$t('shopFeature.chooseFeature.createTime')"
          />
          <el-table-column
            prop="operation"
            :label="$t('text.menu')"
            align="center"
            fixed="right"
            width="120"
          >
            <template #default="scope">
              <div
                class="default-btn text-btn"
                :class="{'disabled-btn': scope.row.reason===4 || scope.row.reason===5 || scope.row.reason===6}"
                @click="handleDetail(scope.row.orderNumber, scope.row.refundSn, scope.row.reason)"
              >
                {{ $t("user.details") }}
              </div>
              <!-- <el-button :disabled="!scope.row.orderNumber && !scope.row.refundSn" type="text" @click="handleDetail(scope.row.orderNumber,scope.row.refundSn)">{{$t('user.details')}}</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页条 -->
      <el-pagination
        v-if="total > 0"
        :current-page="params.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="params.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <div
        class="default-btn"
        @click="goBack()"
      >
        {{ $t("shop.return") }}
      </div>
    </div>
  </div>
</template>

<script setup>
const Data = reactive({
  theData: null, // 保存上次点击查询的请求条件

  time: '',
  activeName: 'second',
  total: 0,
  params: {
    current: 1,
    size: 10
  },
  ioType: [{
    value: 0,
    label: $t('shop.pay')
  }, {
    value: 1,
    label: $t('shop.income')
  }],
  amountType: [{
    value: 0,
    label: $t('shop.amoSett')

  }, {
    value: 1,
    label: $t('shop.avaStoAmo')
  }, {
    value: 2,
    label: $t('shopWallet.unusableBalance')
  }], // '未结算金额', '可提现金额', '冻结金额'
  reason: [
    {
      value: 0,
      label: $t('shop.userPay')
    },
    {
      value: 1,
      label: $t('shopWallet.orderSettlement')
    },
    {
      value: 2,
      label: $t('shopWallet.applyRefund')
    },
    {
      value: 4,
      label: $t('shop.withApply')
    },
    {
      value: 5,
      label: $t('shop.withNoPass')
    },
    {
      value: 6,
      label: $t('shop.withPass')
    }
  ],
  dataFrom: {
    ioType: '',
    amountType: '',
    orderNumber: '',
    refundSn: '',
    reason: ''
  },
  tableData: [{
    balance: 111
  }]
})

const { total, params, ioType, amountType, reason, dataFrom, tableData } = toRefs(Data)

onMounted(() => {
  getData()
})

const route = useRoute()
// 获取列表数据
const getData = () => {
  const params = {
    ...Data.params,
    ...Data.dataFrom,
    shopId: route.query.shopId,
    endTime: route.query.endTime,
    startTime: route.query.startTime
  }
  http({
    url: http.adornUrl('/shop/shopWallet/pageAllShop'),
    method: 'get',
    params
  }).then(({ data }) => {
    Data.tableData = data.records
    Data.total = data.total
  })
}

// 查询
const onSubmit = (newData = false) => {
  Data.dataFrom.orderNumber = Data.dataFrom.orderNumber.trim()
  Data.dataFrom.refundSn = Data.dataFrom.refundSn.trim()
  Data.params.current = 1
  getData(newData)
}

const handleSizeChange = (val) => {
  Data.params.size = val
  getData()
}

const handleCurrentChange = (val) => {
  Data.params.current = val
  getData()
}

const router = useRouter()
// 跳转到详情页
const handleDetail = (orderNumber, refundSn, reason) => {
  if (reason === 4 || reason === 5 || reason === 6) return
  router.push({
    path: '/finance/billing-details/index',
    query: {
      path: 0,
      orderNumber,
      refundSn
    }
  })
}

const goBack = () => {
  router.go(-1)
}

// 清空查询 的表单
const clear = () => {
  Data.dataFrom = {
    ioType: '',
    amountType: '',
    orderNumber: '',
    refundSn: '',
    reason: ''
  }
}
</script>

<style lang="scss" scoped>
</style>
